# Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
#
# Licensed under the BSD 3-Clause License (the "License"); you may not use this
# file except in compliance with the License. You may obtain a copy of the
# License at
#
# https://opensource.org/licenses/BSD-3-Clause
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

cc_library(
    name = "rpc",
    hdrs = [
        "http_filter.h",
        "http_handler.h",
        "logging.h",
        "rpc_channel.h",
        "rpc_client_controller.h",
        "rpc_server_controller.h",
        "server.h",
        "server_group.h",
    ],
    visibility = ["//visibility:public"],
    deps = [
        # Basic classes.
        ":server_group",
        ":server",
        ":http",
        # Frequently used NSLBs.
        "//flare/rpc/load_balancer:round_robin",
        "//flare/rpc/name_resolver:list",
        # Frequently used protocols.
        "//flare/rpc/protocol/http",
        "//flare/rpc/protocol/protobuf",
    ],
)

exports_files(["rpc_options.proto"])

proto_library(
    name = "rpc_options_proto",
    srcs = ["rpc_options.proto"],
    visibility = ["//visibility:public"],
    deps = ["@com_google_protobuf//:descriptor_proto"],
)

cc_proto_library(
    name = "rpc_options_cc_proto",
    visibility = ["//visibility:public"],
    deps = [":rpc_options_proto"],
)

cc_library(
    name = "protobuf",
    hdrs = [
        "rpc_channel.h",
        "rpc_client_controller.h",
        "rpc_server_controller.h",
    ],
    visibility = ["//visibility:public"],
    deps = ["//flare/rpc/protocol/protobuf"],
)

cc_library(
    name = "http",
    hdrs = [
        "http_filter.h",
        "http_handler.h",
    ],
    visibility = ["//visibility:public"],
    deps = [
        "//flare/rpc/protocol/http",
        "//flare/rpc/protocol/http:http_filter",
        "//flare/rpc/protocol/http:http_handler",
    ],
)

cc_library(
    name = "server",
    srcs = [
        "server.cc",
        "//flare/rpc/internal:dry_run_connection_handler.cc",
        "//flare/rpc/internal:dry_run_connection_handler.h",
        "//flare/rpc/internal:normal_connection_handler.cc",
        "//flare/rpc/internal:normal_connection_handler.h",
        "//flare/rpc/internal:server_connection_handler.h",
    ],
    hdrs = [
        "server.h",
    ],
    deps = [
        ":http",
        "//flare/base:chrono",
        "//flare/base:deferred",
        "//flare/base:down_cast",
        "//flare/base:exposed_var",
        "//flare/base:handle",
        "//flare/base:likely",
        "//flare/base:maybe_owning",
        "//flare/base:string",
        "//flare/base:type_index",
        "//flare/base/experimental:uuid",
        "//flare/base/internal:dpc",
        "//flare/base/internal:test_prod",
        "//flare/base/net:endpoint",
        "//flare/fiber",
        "//flare/io:io_basic",
        "//flare/io:stream_connection",
        "//flare/io/native",
        "//flare/io/util:rate_limiter",
        "//flare/io/util:socket",
        "//flare/rpc/binlog",
        "//flare/rpc/binlog:log_reader",
        "//flare/rpc/internal:session_context",
        "//flare/rpc/internal:stream_io_adaptor",
        "//flare/rpc/protocol:stream_protocol",
        "//flare/rpc/protocol:stream_service",
        "//flare/rpc/protocol/http",
        "//flare/rpc/protocol/http/builtin",
        "//flare/rpc/protocol/protobuf",
        "//flare/rpc/protocol/protobuf:service",
        "//flare/rpc/tracing",
        "@com_github_gflags_gflags//:gflags",
        "@com_github_jsoncpp//:jsoncpp",
        "@com_github_opentracing_opentracing_cpp//:opentracing",
    ],
)

cc_library(
    name = "server_group",
    srcs = ["server_group.cc"],
    hdrs = [
        "server_group.h",
    ],
    deps = [
        ":server",
        "//flare/base:maybe_owning",
        "//flare/base/net:endpoint",
    ],
)

cc_library(
    name = "logging",
    hdrs = ["logging.h"],
    visibility = ["//visibility:public"],
    deps = [
        "//flare/fiber:logging",
    ],
)

cc_library(
    name = "message_dispatcher_factory",
    srcs = ["message_dispatcher_factory.cc"],
    hdrs = ["message_dispatcher_factory.h"],
    visibility = ["//visibility:public"],
    deps = [
        "//flare/base:function",
        "//flare/base:never_destroyed",
        "//flare/base/internal:hash_map",
        "//flare/base/internal:macro",
        "//flare/rpc/load_balancer",
        "//flare/rpc/message_dispatcher",
        "//flare/rpc/message_dispatcher:composited",
        "//flare/rpc/name_resolver",
    ],
)

cc_test(
    name = "message_dispatcher_factory_test",
    srcs = ["message_dispatcher_factory_test.cc"],
    deps = [
        ":message_dispatcher_factory",
        "//flare/base:string",
        "//flare/rpc/load_balancer",
        "//flare/rpc/message_dispatcher",
        "//flare/rpc/message_dispatcher:composited",
        "//flare/rpc/name_resolver",
        "@com_google_googletest//:gtest_main",
    ],
)

#############################################
# TARGET BELOW ARE FOR INTERNAL USE.        #
#                                           #
# YOU SHOULD ALWAYS DEPEND ON TARGET `rpc`. #
#############################################

cc_library(
    name = "for_internal_use_only_protobuf_plugin_deps",
    hdrs = [],
    visibility = ["//visibility:public"],
    deps = [
        "//flare/base:expected",
        "//flare/base:future",
        "//flare/base:maybe_owning",
        "//flare/base:status",
        "//flare/rpc/protocol/protobuf",
    ],
)

#  Not implemented yet.
# c_library(
#  name = 'datagram_rpc_server',
#  hdrs = 'datagram_rpc_server.h',
#  srcs = ['datagram_rpc_server.cc'],
#  deps = ['//flare/io:io'],

# c_test(
#  name = 'datagram_rpc_server_test',
#  srcs = [],
#  deps = []

cc_test(
    name = "server_test",
    srcs = ["server_test.cc"],
    deps = [
        ":http",
        ":rpc",
        "//flare/fiber",
        "//flare/net/http:http_client",
        "//flare/rpc/internal:stream_call_gate",
        "//flare/rpc/protocol:stream_service",
        "//flare/testing:echo_service_proto_flare",
        "//flare/testing:endpoint",
        "//flare/testing:main",
        "@com_github_jsoncpp//:jsoncpp",
    ],
)

cc_test(
    name = "server_group_test",
    srcs = ["server_group_test.cc"],
    deps = [
        ":rpc",
        ":server_group",
        "//flare/net/http:http_client",
        "//flare/rpc:server",
        "//flare/testing:echo_service_proto_flare",
        "//flare/testing:endpoint",
        "//flare/testing:main",
    ],
)

cc_test(
    name = "integration_test",
    srcs = ["integration_test.cc"],
    # Internally we also redistribute this UT to dedicated test machine. Linking
    # statically make redistribution a bit easier.
    # linkshared = False,
    linkstatic = True,
    deps = [
        ":rpc",
        "//flare/base:down_cast",
        "//flare/base:random",
        "//flare/base/internal:cpu",
        "//flare/fiber",
        "//flare/init:override_flag",
        "//flare/testing:echo_service_proto_flare",
        "//flare/testing:endpoint",
        "//flare/testing:main",
        "//flare/testing:relay_service_proto_flare",
    ],
)

cc_test(
    name = "logging_test",
    srcs = ["logging_test.cc"],
    deps = [
        ":rpc",
        "//flare/testing:echo_service_proto_flare",
        "//flare/testing:endpoint",
        "//flare/testing:main",
    ],
)
